/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * URLLoadAndShowPanel.java
 *
 * Created on 2009-1-17, 23:41:56
 */

package com.guzzservices.easydomain.ui.panel;

import com.guzzservices.easydomain.ui.render.OnePageHtmlRenderContext;
import com.guzzservices.easydomain.util.HelpUtil;
import com.guzzservices.easydomain.util.NetUtil;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import org.lobobrowser.html.gui.HtmlPanel;
import org.lobobrowser.html.test.SimpleUserAgentContext;
import org.w3c.dom.html2.HTMLElement;

/**
 *
 * @author Administrator
 */
public class URLLoadAndShowPanel extends javax.swing.JPanel {

    public static void main(String args[]){
        JFrame f = new JFrame() ;
        f.setSize(600, 600) ;
        f.getContentPane().add(new URLLoadAndShowPanel("http://www.google.com/")) ;
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

    private String urlToLoad ;

    /** Creates new form URLLoadAndShowPanel */
    public URLLoadAndShowPanel() {
        initComponents();
    }

    public URLLoadAndShowPanel(String url) {
        this.urlToLoad = url ;
        initComponents();
        try{
            initData() ;
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    private void initData(){
        jScrollPane1.setViewportView(panel);
        reloadPage() ;
    }

    public void reloadPage(){
        final OnePageHtmlRenderContext context = new OnePageHtmlRenderContext(panel, new SimpleUserAgentContext()){

            public void linkClicked(HTMLElement linkNode, URL url, String target) {
                if("native".equalsIgnoreCase(target)){
                    try {
                        HelpUtil.openURLInSystemBroswer(url.toURI());
                    } catch (URISyntaxException ex) {
                        Logger.getLogger(URLLoadAndShowPanel.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }else{
                    super.linkClicked(linkNode, url, target);
                }
            }
           
        } ;
        
        Thread t = new Thread(
            new Runnable(){

            public void run() {
                  try {
                        panel.setHtml(NetUtil.getPageFromServer(urlToLoad), urlToLoad, context);
                        panel.repaint();
                    } catch (Exception ex) {
                        Logger.getLogger(URLLoadAndShowPanel.class.getName()).log(Level.SEVERE, null, ex);
                    }
            }
        }) ;

        t.start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();

        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 535, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    private HtmlPanel panel = new HtmlPanel();
}
